package com.princess.demok.view

import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.princess.baselib.utils.LogUtils
import com.princess.demok.R

/**
 * @author: xha
 * @date: 2021/10/26 11:12
 * @Description:
 */
class CanvasView : View {
    constructor(context: Context) : super(context) {
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {

    }

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
        context,
        attributeSet,
        defStyleAttr
    ) {

    }

    private var mPaint: Paint = Paint()

    init {
        mPaint.color = ContextCompat.getColor(context, R.color.color_blue)
        mPaint.setStrokeWidth(dpToPixel(3f))
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas?.drawLine(50f, 50f, 300f, 300f, mPaint)

        val lines = floatArrayOf(60f, 60f, 150f, 100f, 100f, 100f, 200f, 100f)
        canvas?.drawLines(lines, mPaint)

        canvas?.drawLine(dpToPixel(100f), dpToPixel(100f), dpToPixel(200f), dpToPixel(200f), mPaint)

        canvas?.drawPoint(dpToPixel(120f),dpToPixel(120f),mPaint)

        mPaint.style = Paint.Style.STROKE

        canvas?.drawRect(Rect(150,350,200,500),mPaint)

        canvas?.drawCircle(width/2f,height/4f,80f,mPaint)

        canvas?.drawOval(RectF(800f,300f,1000f,400f),mPaint)
    }

    private fun dpToPixel(dp: Float): Float {
        val metrics = Resources.getSystem().displayMetrics
        LogUtils.i("密度"+metrics.density)
        return dp * metrics.density
    }
}